package com.caiheng.api.service.base.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.caiheng.api.commons.base.SearchCondition;
import com.caiheng.api.commons.data.DataPage;
import com.caiheng.api.dao.BaseDao;
import com.caiheng.api.service.base.BaseService;
import com.caiheng.api.util.MapperUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;
import java.util.Map;

@SuppressWarnings("unchecked")
public abstract class BaseServiceImpl<M extends BaseDao<T>, T> extends ServiceImpl<M, T> implements BaseService<T> {

    protected Logger logger = LoggerFactory.getLogger(getClass());

    @Resource
    protected M baseDao;

    @Override
    public T findById(Serializable id) {
        return getById(id);
    }

    /**
     * 根据map参数查询数量
     *
     * @param params
     * @return
     */
    @Override
    public long selectCountByMap(Map<String, Object> params) {
        return baseDao.selectCountByMap(params);
    }

    /**
     * 根据一列值查询数量
     * @param column
     * @param value
     * @return
     */
    @Override
    public long selectCountByColumn(String column, Object value) {
        return baseDao.selectCountByColumn(column, value);
    }


    /**
     * 通过一列值查询
     *
     * @return
     */
    @Override
    public List<T> selectByMap(Map<String, Object> params){
        return baseDao.selectByMap(params);
    }

    /**
     * 通过一列值查询
     *
     * @param column
     * @param value
     * @return
     */
    @Override
    public List<T> selectListByColumn(String column, Object value) {
        QueryWrapper<T> qw = new QueryWrapper<T>();
        qw.eq(column,value);
        return list(qw);
//        return baseDao.selectListByColumn(column, value);
    }

    /**
     * 通过一唯一列值查询唯一值
     *
     * @param column
     * @param value
     * @return
     */
    @Override
    public T selectByUniqueColumn(String column, Object value) {
        QueryWrapper<T> qw = new QueryWrapper<T>();
        qw.eq(column,value);
        return getOne(qw);
    }

    /**
     * 查询表上所有数据
     *
     * @return
     */
    @Override
    public List<T> selectAllList() {
        return baseDao.selectAllList();
    }


    public List<T> getPageEntityList(QueryWrapper queryWrapper, int pageNum, int pageSize) {
        Page<T> page = new Page<>(pageNum, pageSize);
        IPage<T> list = page(page,queryWrapper);
        return list.getRecords();
    }

    /**
     * 分页 搜索
     *
     * @param
     * @return
     */
    @Override
    public List<T> selectByPage(DataPage<T> condition) {
        Page<T> page = new Page<T>(condition.getPage(), condition.getLimit());
        SearchCondition cnd = SearchCondition.create();
        if(condition.getQw()!=null){
            MapperUtil.loadSort(condition.getSorts(), condition.getQw());
            page(page, condition.getQw());
        }else{
            MapperUtil.loadSearchParam(condition.getSearchParams(), cnd);
            MapperUtil.loadSort(condition.getSorts(), cnd);
            page(page, cnd);
        }
        return page.getRecords();
    }



    @Override
    public List<T> selectOptionsList(String params) {
        return null;
    }

    @Override
    public List<T> selectOptionsList() {
        return null;
    }
}
